/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Xiaobao Zeng<zengxiaobao@skyworth.com>
 * Created Time: 2016-07-07 14:21:09
 * Last Modified: 2016-07-21 19:46:35
 * File Name: servicepageinfomgr.go
 * Description:
 */

package controllers

import (
	"encoding/json"
	"net/url"
	"strconv"

	"advertoper/errorcode"
	"advertoper/hotactivity"

	log "github.com/cihub/seelog"
)

type HotActivityInfoMgrController struct {
	SCLoginedController
}

func (p *HotActivityInfoMgrController) CreateHotActivityInfo() {
	defer p.Output("SaveHotActivityInfo")
	body := p.Ctx.Input.RequestBody
	log.Debugf("recv body %s", string(body))

	req := &hotactivity.HotActivityInfo{}
	if err := json.Unmarshal(body, req); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	if ok := hotactivity.GetHotActivityInfoService().CreateHotActivityInfo(req); !ok {
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = "创建热门活动失败"
		return
	}
	p.Resp["code"] = errorcode.EC_GL_SUCCESS
	p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
}

func (p *HotActivityInfoMgrController) GetHotActivityInfoList() {
	defer p.Output("GetHotActivityInfoList")

	body := p.Ctx.Input.RequestBody
	qs, _ := url.ParseQuery(string(body))
	length, _ := strconv.Atoi(qs.Get("length"))
	if length == 0 {
		length = 25
	}
	start, _ := strconv.Atoi(qs.Get("start"))

	data, total := hotactivity.GetHotActivityInfoService().ListHotActivity(start, length)
	p.Resp["code"] = errorcode.EC_GL_SUCCESS
	p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
	p.Resp["draw"], _ = strconv.Atoi(qs.Get("draw"))
	p.Resp["data"] = data
	p.Resp["recordsTotal"] = total
	p.Resp["recordsFiltered"] = total

}

func (p *HotActivityInfoMgrController) ModifyHotActivityInfo() {
	defer p.Output("ModifyHotActivityInfo")
	body := p.Ctx.Input.RequestBody
	log.Debugf("recv body %s", string(body))

	req := &hotactivity.HotActivityInfo{}
	if err := json.Unmarshal(body, req); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	if req.ActivityId == 0 {
		p.Resp["code"] = errorcode.EC_HOTACTIVITY_AID_MISSING
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_HOTACTIVITY_AID_MISSING)
		return
	}

	if ok := hotactivity.GetHotActivityInfoService().ModifyHotActivityInfo(req); !ok {
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = "修改失败"
		return
	}
	p.Resp["code"] = errorcode.EC_GL_SUCCESS
	p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
}

func (p *HotActivityInfoMgrController) DeleteHotActivityInfo() {
	defer p.Output("DeleteHotActivityInfo")
	body := p.Ctx.Input.RequestBody
	log.Debugf("recv body %s", string(body))

	bodyData := make(map[string]interface{})
	if err := json.Unmarshal(body, &bodyData); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	var aid int64
	if val, ok := bodyData["activity_id"]; ok {
		aid = int64(val.(float64))
	} else {
		p.Resp["code"] = errorcode.EC_HOTACTIVITY_AID_MISSING
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_HOTACTIVITY_AID_MISSING)
		return
	}
	if ok := hotactivity.GetHotActivityInfoService().DeleteHotActivityInfo(aid); !ok {
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = "删除失败"
		return
	}
	p.Resp["code"] = errorcode.EC_GL_SUCCESS
	p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
}
